package org.duang.db.sql.parser.encode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.duang.db.mongo.Operator;
import org.duang.db.sql.SqlObject;
import org.duang.db.sql.parser.SqlEncodeConvetor;
import org.duang.kit.ToolsKit;

import com.mongodb.DBObject;

public class SqlDBObjectEncoder extends SqlEncoder {

	public SqlDBObjectEncoder(String key,String opt, Object dbo) {
		super(key, opt, dbo);
	}


	@Override
	public  List<SqlObject> getValue() {
		DBObject dbo = (DBObject)obj;
		List<SqlObject> sqlObjectList = new ArrayList<SqlObject>();
		for(Iterator<String> it = dbo.keySet().iterator(); it.hasNext();){
			String name = it.next();
			Object value = dbo.get(name);
			sqlObjectList.addAll(SqlEncodeConvetor.parser(getKey(name), name, value).getValue());
		}
		return sqlObjectList;
	}

	private String getKey(String name) {
		if(key.indexOf("$")> -1){
			return name;
		} else {
			return key;
		}
	}
	
}
